package com.hymjweb.demo.hymj.basic_data_manage.xt_company.service;

import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_company.bean.XtCompanyCachePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_company.bean.XtCompanyPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_company.mapper.XtCompanyMapper;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.basic_data_manage.xt_company.service
 * @Author: luoge
 * @CreateTime: 2020-10-11 21:46
 * @Description:
 */
@Service
public class XtCompanyService extends BaseService {

    @Autowired
    private XtCompanyMapper xtCompanyMapper;

    @Autowired
    private GlobalCacheService globalCacheService;

    public List<XtCompanyCachePojo> findCacheAll() {
        List<XtCompanyCachePojo> pojos = xtCompanyMapper.findCacheAll();
        return pojos;
    }

    public Object getPageList(Map map) {
        this.pageListInit(map);
        List list = xtCompanyMapper.findAll(map);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    @OperationLogger(oper = SysConstant.OPER_ADD, operTitle = "公司-新增", operMemo = "", classType = XtCompanyPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(XtCompanyPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("公司新增参数错误");
        }

        // 设置公司对象ID
        pojo.setId(this.getSysStrId());

        int affectRows = xtCompanyMapper.insert(pojo);
        if (affectRows > SysConstant.INT_ZERO) {
            globalCacheService.loadUnitCache();
        }

        return affectRows;
    }

    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle = "公司-更新", operMemo = "", classType = XtCompanyPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int update(XtCompanyPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("公司更新参数错误");
        }
        int affectRows = xtCompanyMapper.update(pojo);

        if (affectRows > SysConstant.INT_ZERO) {
            globalCacheService.loadUnitCache();
        }
        return affectRows;
    }

    //@OperationLogger(oper = SysConstant.OPER_DELETE, operTitle = "公司-删除", operMemo = "", classType = XtCompanyPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int delete(String id) throws Exception {
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("公司删除参数错误");
        }
        int affectRows = xtCompanyMapper.delete(id);
        if (affectRows > SysConstant.INT_ZERO) {
            globalCacheService.loadUnitCache();
        }
        return affectRows;
    }

}
